<template>
    <div class="container">
        <div class='u-title f-mtp10'>
            <h2>添加广告</h2>
        </div>

        <div class='u-content mn flex flex-column flex-center'>
            <div class='item'>
                <label for="">广告名称：</label>
                <div class='item-right'>
                    <el-input v-model="title" placeholder="案例标题"></el-input>
                    <p class='tip'>广告名称只是作为辨别多个广告条目之用，并不显示在广告中</p>
                </div>
            </div>
            <div class='item'>
                <label for="">广告位置：</label>
                <div class='item-right'>
                    <el-select class='f-w-full' v-model="departmentId" clearable placeholder="请选择">
                        <el-option
                        v-for="item in list"
                        :key="item.id"
                        :label="item.label"
                        :value="item.id">
                        </el-option>
                    </el-select>
                </div>
            </div>
            <div class='item'>
                <label for="">上线/下线：</label>
                <div class='item-right radio-box'>
                  <el-radio v-model="radio" label="1">上线</el-radio>
                  <el-radio v-model="radio" label="2">下线</el-radio>
                </div>
            </div>
            <div class='item'>
                <label for="">广告图片：</label>
                <div class='item-right'>
                    <p class='tip'>只能上传jpg/png格式文件，文件不能超过50kb</p>
                </div>
            </div>
            <div class='item'>
                <label for="">广告链接：</label>
                <div class='item-right'>
                    <el-input v-model="title" placeholder="链接地址"></el-input>
                </div>
            </div>
            <div class='btn-box f-ptp20'>
                <el-button
                    class='f-w300 s-bg-green s-cfff'
                    @click="submit">提交</el-button>
            </div>
        </div>
    </div>
</template>

<script>
import axios from 'axios';
export default {
  name: 'AddAd',
  data() {
    return {
      radio: '1',
      departmentId: '',  
      list: [
        {
          id: 1,
          label: 'APP首页轮播'
        },
        {
          id: 2,
          label: 'PC端首页轮播'
        },
        {
          id: 3,
          label: 'PC端分类广告'
        }
      ],  // 部门列表  
      phone: '',
      password: '',
      rePassword: '',
      name: '',  
    }
  },
  computed: {
  
  },
  created: function() {
    this.getDepartmentList();
  },
  methods: {
    // 
    submit() {
      this.createStaff();  
    },  
    // 添加员工 
    createStaff() {
      console.log('createStaff-------------', this.name);  
      const _this = this;
      const params = new URLSearchParams();
      params.append('phone', this.phone);
      params.append('name', this.name);
      params.append('departmentId', this.departmentId);
      params.append('password', this.password);
      params.append('rePassword', this.rePassword);
      axios.post(`${this.GLOBAL.baseUrl}/api/admin/createStaff`, params, {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      })
      .then((res) => {
        console.log(res);
        if (res.data.returnCode === 200) {
        
          _this.$message({
            message: '恭喜你，修改成功！',
            type: 'success'
          });
          this.$router.push('/home/organizationalStructure');
        //   _this.getList();
        } else {
          _this.$message({
            message: res.data.message,
            type: 'warning'
          });
        }
      })
      .catch((error) => {
        console.log(error);
      });  
    }, 
    // 获取部门列表  
    getDepartmentList() {
      const _this = this;
      axios.get(`${this.GLOBAL.baseUrl}/api/admin/getDepartmentList`).then(res => {
        if(res.data.returnCode == 200) {
          console.log('list:::', res.data.data);
          _this.departmentList = res.data.data;
        }
      }).catch(err => {
        console.error('err::', err);
      })  
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.radio-box{
  line-height: 40px;
}
.container{
  width: 800px;
  /* margin: 0 auto; */
}
.mn{
    padding: 30px 0 50px 0;
}
.item{
    display: flex;
    margin-bottom: 10px;
}
.item>label{
    display: inline-block;
    width: 90px;
    line-height: 40px;
    text-align: right;
    margin-right: 10px;
}
.item>label::before{
    content: '*';
    line-height: 40px;
    color: red;
    margin-right: 5px;
}
.item-right{
    width: 600px
}
.btn-box{
    padding-left: 90px;
}
.tip{
    font-size: 12px;
    color: #999;
    margin-bottom: 10px;
    margin-top: 10px;
    /* line-height: 24px; */
}
</style>
